package com.dataex.frame;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.dataex.service.DBService;
import com.dataex.service.XmlToDBService;

public class ImportFrame extends JFrame implements ItemListener{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String filePath;
	
	private final static String BASEURL="../xmlDemo/images/";
	
	private JComboBox comboBox;
	
	private List<String> list;
	
	private String[] arrs = {};
	
	
	private String dbName ="xml";
	
	public ImportFrame(){
		 JFileChooser fileChooser=new JFileChooser("打开文件");
         int isOpen=fileChooser.showOpenDialog(null);
         fileChooser.setDialogTitle("打开文件");
         if(isOpen==JFileChooser.APPROVE_OPTION){
         	filePath = fileChooser.getSelectedFile().getPath();
         	
         	//final JDialog dialog=new JDialog();
         	Image image=Toolkit.getDefaultToolkit().getImage(BASEURL+"logo.png");
     		setIconImage(image);
     		setTitle("导入信息");
             
         	JPanel p1=new JPanel();
            JPanel p2=new JPanel();
             
         	
             JTextArea textArea=new JTextArea(60,60);
         	textArea.setText(readFromFile(filePath));
         	JScrollPane scrollPanel=new JScrollPane(textArea);
         	scrollPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
         	scrollPanel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
         	scrollPanel.getViewport().add(textArea);
         	scrollPanel.getViewport().setPreferredSize(new Dimension(700,570));
         	JButton yes=new JButton("导入");
         	JButton no=new JButton("取消");
         	
         	yes.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						
						try {
							if(dbName.equals("xml")){
								new XmlToDBService().importDataIntoDB(filePath,dbName);
							}else{
								JOptionPane.showConfirmDialog(null, "当前版本只支持特定数据库","温馨提示",JOptionPane.YES_NO_OPTION);
							}
						} catch (Exception e1) {
							e1.printStackTrace();
						}
						
					}
				});
         	
         	no.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
                      setVisible(false);							
					}
				});
         	
         	
         	JLabel label = new JLabel("数据库:");
         	
         	try {
				list = new DBService().getAllDatabases();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
         	
         	arrs = new String[list.size()];
         	
         	for(int i = 0; i < list.size(); i++){
         		arrs[i] = list.get(i);
         	}
         	
         	comboBox = new JComboBox(arrs);
         	comboBox.setSelectedItem(dbName);
         	comboBox.addItemListener(this);
         	
         	p1.add(scrollPanel);
         	
         	p2.setLayout(new FlowLayout(FlowLayout.LEFT));
         	p2.add(label);
         	p2.add(comboBox);
         	p2.add(yes);
         	p2.add(no);
         	
         	add("Center",p1);
         	add("South",p2);
         	
         	setVisible(true);
         	setSize(800,700);
         	setLocation(100,100);
         }
	}

	
	public String readFromFile(String path){
		File file=new File(path);
		String s=null;
		try {
		FileInputStream fin=new FileInputStream(file);
		
		int length=fin.available();
		
		byte arr[]=new byte[length];
		
		int len=fin.read(arr);
		
		s=new String(arr,0,len);
		
			
		} catch (FileNotFoundException e) {
		
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		return s;
		
	}


	@Override
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub
		if(e.getStateChange() == ItemEvent.SELECTED){
			dbName =  "" + e.getItem();
		}
	}
}
